package dao;

import bean.Log;
import util.Linksql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Function  {
    private int k=0;
    public boolean choose(String one,String two,String num,String id){//one是开始时间two是结束时间num是座位编号
        try {
            System.out.println(one);
            Date date=new Date();
            Timestamp now= new Timestamp(date.getTime());//获取当前时间
            Timestamp oneone = Timestamp.valueOf(one);//传入的开始时间
            Timestamp twoone =Timestamp.valueOf(two);//结束时间
            System.out.println(oneone);
            List<Log> logList = new ArrayList<>();
            String sqlone = "select * from book_s_rd where readroomnum='"+num+"';";
            String sqltwo = "select * from book_s_sn where seminarnum='"+num+"';";
            String sqlthree = "select * from book_s_st where seatnum='"+num+"';";
            String sqlfour = "select * from book_t_rd where readroomnum='"+num+"';";
            String sqlfive = "select * from book_t_sn where seminarnum='"+num+"';";
            String sqlsix = "select * from book_t_st where seatnum='"+num+"';";
            Connection connection = Linksql.getConnection();//自定义函数连接数据库
            PreparedStatement prep = connection.prepareStatement(sqlone);
            ResultSet resultSet = prep.executeQuery();
            while(resultSet.next()){
                Log seat=new Log();
                seat.setId(resultSet.getString("stuid"));
                seat.setNum(resultSet.getString("readroomnum"));
                seat.setStart_time(String.valueOf(resultSet.getTimestamp("starttime")));
                seat.setEnd_time(String.valueOf(resultSet.getTimestamp("endtime")));
                seat.setStatus(String.valueOf(resultSet.getString("status")));
                logList.add(seat);
            }
            prep = connection.prepareStatement(sqltwo);
            resultSet=prep.executeQuery();
            while(resultSet.next()){
                Log seat=new Log();
                seat.setId(resultSet.getString("stuid"));
                seat.setNum(resultSet.getString("seminarnum"));
                seat.setStart_time(String.valueOf(resultSet.getTimestamp("starttime")));
                seat.setEnd_time(String.valueOf(resultSet.getTimestamp("endtime")));
                seat.setStatus(String.valueOf(resultSet.getString("status")));
                logList.add(seat);
            }
            prep = connection.prepareStatement(sqlthree);
            resultSet=prep.executeQuery();
            while(resultSet.next()){
                Log seat=new Log();
                seat.setId(resultSet.getString("stuid"));
                seat.setNum(resultSet.getString("seatnum"));
                seat.setStart_time(String.valueOf(resultSet.getTimestamp("starttime")));
                seat.setEnd_time(String.valueOf(resultSet.getTimestamp("endtime")));
                seat.setStatus(String.valueOf(resultSet.getString("status")));
                logList.add(seat);
            }
            prep = connection.prepareStatement(sqlfour);
            resultSet=prep.executeQuery();
            while(resultSet.next()){
                Log seat=new Log();
                seat.setId(resultSet.getString("teacherid"));
                seat.setNum(resultSet.getString("readroomnum"));
                seat.setStart_time(String.valueOf(resultSet.getTimestamp("starttime")));
                seat.setEnd_time(String.valueOf(resultSet.getTimestamp("endtime")));
                seat.setStatus(String.valueOf(resultSet.getString("status")));
                logList.add(seat);
            }
            prep = connection.prepareStatement(sqlfive);
            resultSet=prep.executeQuery();
            while(resultSet.next()){
                Log seat=new Log();
                seat.setId(resultSet.getString("teacherid"));
                seat.setNum(resultSet.getString("seminarnum"));
                seat.setStart_time(String.valueOf(resultSet.getTimestamp("starttime")));
                seat.setEnd_time(String.valueOf(resultSet.getTimestamp("endtime")));
                seat.setStatus(String.valueOf(resultSet.getString("status")));
                logList.add(seat);
            }
            prep = connection.prepareStatement(sqlsix);
            resultSet=prep.executeQuery();
            while(resultSet.next()){
                Log seat=new Log();
                seat.setId(resultSet.getString("teacherid"));
                seat.setNum(resultSet.getString("seatnum"));
                seat.setStart_time(String.valueOf(resultSet.getTimestamp("starttime")));
                seat.setEnd_time(String.valueOf(resultSet.getTimestamp("endtime")));
                seat.setStatus(String.valueOf(resultSet.getString("status")));
                logList.add(seat);
            }

                sqlone = "select * from student where stuid='"+id+"'";
                sqltwo = "select * from teacher where teacherid='"+id+"'";

                sqlthree = "select * from seat where seatnum='"+num+"'";
                sqlfour = "select * from seminar where seminarnum='"+num+"'";
                sqlfive = "select * from readroom where readroomnum='"+num+"'";
                prep = connection.prepareStatement(sqlone);
                ResultSet a = prep.executeQuery();
                prep = connection.prepareStatement(sqltwo);
                ResultSet b = prep.executeQuery();
                prep = connection.prepareStatement(sqlthree);
                ResultSet c = prep.executeQuery();
                prep = connection.prepareStatement(sqlfour);
                ResultSet d = prep.executeQuery();
                prep = connection.prepareStatement(sqlfive);
                ResultSet e = prep.executeQuery();
                if (a.next()){
                    if (c.next()){
                        k=3;
                    } else if (d.next()) {
                        k=2;
                    }else if (e.next()){
                        k=1;
                    }
                } else if (b.next()) {
                    if (c.next()){
                        k=6;
                    } else if (d.next()) {
                        k=5;
                    }else if (e.next()){
                        k=4;
                    }
                }

            for(Log log:logList){
                if((log.getStatus().equals("未签到"))||(log.getStatus().equals("未签离"))){
                    if(!((oneone.getTime()>Timestamp.valueOf(log.getEnd_time()).getTime())||
                            (twoone.getTime()<Timestamp.valueOf(log.getStart_time()).getTime()))){
                        //预约时间冲突
                        System.out.println("return掉了");
                        return false;
                    }
                }
            }
            System.out.println(k+"我是k值");
System.out.println("我倒要看看你执行到哪 了");
            if (k==1){//s_rd
                String sql_insert = "insert into book_s_rd(stuid,readroomnum,starttime,endtime,submittime,status) values(?,?,?,?,?,?); ";
                prep = connection.prepareStatement(sql_insert);
                prep.setString(1,id);
                prep.setString(2,num);
                prep.setTimestamp(3,oneone);
                prep.setTimestamp(4,twoone);
                prep.setTimestamp(5,now);
                prep.setString(6,"未签到");
                prep.execute();//执行sql语句

            }else if (k==2){//s_sn
                String sql_insert = "insert into book_s_sn(stuid,seminarnum,starttime,endtime,submittime,status) values(?,?,?,?,?,?); ";
                prep = connection.prepareStatement(sql_insert);
                prep.setString(1,id);
                prep.setString(2,num);
                prep.setTimestamp(3,oneone);
                prep.setTimestamp(4,twoone);
                prep.setTimestamp(5,now);
                prep.setString(6,"未签到");
                prep.execute();//执行sql语句

            }else if (k==3){//s_st
                String sql_insert = "insert into book_s_st(stuid,seatnum,starttime,endtime,submittime,status) values(?,?,?,?,?,?);";
                prep = connection.prepareStatement(sql_insert);
                prep.setString(1,id);
                prep.setString(2,num);
                prep.setTimestamp(3,oneone);
                prep.setTimestamp(4,twoone);
                prep.setTimestamp(5,now);
                prep.setString(6,"未签到");
                prep.execute();//执行sql语句
                System.out.println("插入执行了咩");
                System.out.println(oneone);
                System.out.println(twoone);

            }else if (k==4){//t_rd
                String sql_insert = "insert into book_t_rd(teacherid,readroomnum,starttime,endtime,submittime,status) values(?,?,?,?,?,?); ";
                prep = connection.prepareStatement(sql_insert);
                prep.setString(1,id);
                prep.setString(2,num);
                prep.setTimestamp(3,oneone);
                prep.setTimestamp(4,twoone);
                prep.setTimestamp(5,now);
                prep.setString(6,"未签到");
                prep.execute();//执行sql语句

            }else if (k==5){//t_sn
                String sql_insert = "insert into book_t_sn(teacherid,seminarnum,starttime,endtime,submittime,status) values(?,?,?,?,?,?); ";
                prep = connection.prepareStatement(sql_insert);
                prep.setString(1,id);
                prep.setString(2,num);
                prep.setTimestamp(3,oneone);
                prep.setTimestamp(4,twoone);
                prep.setTimestamp(5,now);
                prep.setString(6,"未签到");
                prep.execute();//执行sql语句

            } else if(k==6){//t_st
                String sql_insert = "insert into book_t_st(teacherid,seatnum,starttime,endtime,submittime,status) values(?,?,?,?,?,?); ";
                prep = connection.prepareStatement(sql_insert);
                prep.setString(1,id);
                prep.setString(2,num);
                prep.setTimestamp(3,oneone);
                prep.setTimestamp(4,twoone);
                prep.setTimestamp(5,now);
                prep.setString(6,"未签到");
                prep.execute();//执行sql语句

            }
            prep.close();
            resultSet.close();
            connection.close();

//符合要求提交预约
        }catch (Exception e){
            e.printStackTrace();
        }
        return true;
    }
}
